/*
File Name: blinkingled.c
Written by: Sam Lee
December 15 2009
*/
/*
FRC Oscillator with PLL
FRC = 7.37 MHz,
References: DS70291D-page 146
*/
	                	
#include <p33FJ64MC802.h>

_FOSCSEL(FNOSC_FRCPLL);			//Fast RC Oscillator with PLL
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF  & POSCMD_NONE); 
_FWDT(FWDTEN_OFF);				//WDT Off
_FGS(GCP_OFF);					//Code Protect Off

void delay_ms(int wTime){
	//clock(Tcy)=36.85MHz 1clock=27.137nS
	int i;
	T1CONbits.TCKPS = 1;		//1/8 @36.85MHz (Timer1 Input Clock Prescale Select bits) 36.85MHz/8 = 4.60625MHz 
	T1CONbits.TON = 1;			//TON: Timer1 On bit (1 = Starts 16-bit Timer1)
	for(i=0; i<wTime; i++){
		TMR1 = 0;					//Timer1=0
		while(TMR1 < 4606);			//1mSec
	}
	T1CONbits.TON = 0;			//Stop Timer1
}

int main(void)
{
	CLKDIVbits.PLLPRE = 0;		//N1=1/2
	CLKDIVbits.PLLPOST = 0;		//N2=1/2
	PLLFBD = 38;				//M= PLLFBD+2 = 38+2 =40

								//Fosc = Fin(M*N1*N2) =  FSC(M*N1*N2) = 7.37MHz(40*1/2*1/2) = 73.7MHz
								//Fcy = Fosc/2 = 73.7MHz/2 = 36.85MHz
 
	/* set LED pin14 (RB5) as outputs */
    //TRISB = 0xFFDF;
	TRISA = 0x1E;
	
	while(1){
		/* set LED pins (RB5) ON */
		//PORTB = 0x0020;
        PORTA = 0x01;
		delay_ms(500);
		/* set LED pins (RE7- RE0) OFF */
        //PORTB = 0xFFDF;
        PORTA = 0x1E;
		delay_ms(500);
	}
}
